package com.feiyu.config;

import com.feiyu.bean.SysUser;
import com.feiyu.dao.CommonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author nanshouxiao@teacher.com.cn
 * @date 2017-11-03 16:17
 */
@Service
public class CustomUserDetailService implements UserDetailsService {

    @PostConstruct
    public void test() {
        System.out.println("CustomUserDetailService post ......");
    }

    @Autowired
    private CommonMapper<SysUser> commonMapper;

    private static final String selectUserSql = "SELECT username, password, email, id from sys_user WHERE username = ";

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = commonMapper.selectOne(selectUserSql + "'" + username + "'");
        if (sysUser == null) {
            throw new RuntimeException("error");
        }
        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new SimpleGrantedAuthority("aaa"));
        return new CustomUserDetail(sysUser, list);
    }

}
